package renguanyu.app.codebuilder.dao;

import java.util.Map;

import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import renguanyu.app.codebuilder.util.JDBCFactory;
import renguanyu.app.codebuilder.util.StringUtil;

public class TemplateDao {

    public Map<String,Object> find(String tempId) {
        return null;
    }

    public boolean save(String templateId, String templateName) {
        return JDBCFactory.get().add("insert into temp values('" + templateId + "','" + templateName + "')");
    }

    public ObservableList<ObservableMap<String, String>> query(String templateName) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * ");
        sql.append("FROM TEMP ");
        sql.append("WHERE 1=1 ");
        if(StringUtil.notEmpty(templateName)){
            sql.append("AND TEMP_NAME LIKE '%"+templateName+"%' ");
        }
        return JDBCFactory.get().query(sql.toString());
    }
    public ObservableMap<String, String> queryOne(String templateId) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * ");
        sql.append("FROM TEMP ");
        sql.append("WHERE TEMP_ID = '"+templateId+"' ");
        return JDBCFactory.get().queryOne(sql.toString());
    }
    public boolean delete(String tempId) {
        StringBuilder sql = new StringBuilder();
        sql.append("DELETE FROM TEMP  ");
        sql.append("WHERE TEMP_ID = '"+tempId+"' ");
        boolean flag = JDBCFactory.get().del(sql.toString());
        sql = new StringBuilder();
        sql.append("DELETE FROM TEMP_DATA  ");
        sql.append("WHERE TEMP_ID='"+tempId+"' ");
        flag = JDBCFactory.get().del(sql.toString());
        return flag;
    }

    public boolean update(String templateId, String templateName) {
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE TEMP ");
        sql.append("SET TEMP_NAME = '"+templateName+"' ");
        sql.append("WHERE TEMP_ID = '"+templateId+"' ");
        return JDBCFactory.get().mod(sql.toString());
    }
}
